/*
 * db.c
 *
 *  Created on: 2017年7月3日
 *      Author: zjm
 */

#include "db.h"
//#include "log.h"

#define log_err(...)
#define log_start(...)
#define log_info(...)

int db_open(const char *name,dbFile *h)
{
	int err;

	log_start("db");

	err = sqlite3_open(name,&h->db);
	if(err != SQLITE_OK){
		log_err("ERROR: %s.\n",sqlite3_errmsg(h->db));
		return -1;
	}

  h->stmt = NULL;

	return 0;
}

void db_close(dbFile *h)
{
	sqlite3_close(h->db);
}


int dbStart(dbFile *h)
{
	if(h->stmt){
		sqlite3_finalize(h->stmt);
	}

	h->stmt = NULL;

	return 0;
}

int dbSql(dbFile *h,const char *sql)
{
	int rc = sqlite3_prepare_v2(h->db,sql,-1,&h->stmt,0);
	if(rc != SQLITE_OK){
		log_info("%s\n",sqlite3_errmsg(h->db));
		log_info("sql: %s\n",sql);
		return -1;
	}

	return 0;
}

int dbStep(dbFile *h)
{
	int rc = sqlite3_step(h->stmt);
	return rc;
}

void dbDone(dbFile *h)
{
	if(h->stmt){
		sqlite3_finalize(h->stmt);
	}

	h->stmt = NULL;
}


char *dbGetResultString(dbFile *h,int pos)
{
	return (char *)sqlite3_column_text(h->stmt,pos);
}

uint32_t dbGetResultInt(dbFile *h,int pos)
{
	return (uint32_t)sqlite3_column_int64(h->stmt,pos);
}

float dbGetResultReal(dbFile *h,int pos)
{
	return (float)sqlite3_column_double(h->stmt,pos);
}
